package player

import (
	"fmt"
	"github.com/name5566/leaf/log"
	"go.mongodb.org/mongo-driver/bson"
	db2 "server/db"
)

type PlayerLogic struct {
	dao *PlayerDao
}

func NewPlayerLogic() *PlayerLogic {
	return &PlayerLogic{
		dao: NewPlayerDao(),
	}
}
func (p *PlayerLogic) getPlayerListFromDatabase(arrUid []string) ([]Player, error) {
	return p.dao.getPlayerListFromDatabase(arrUid)
}
func (p *PlayerLogic) getPlayerFromDatabase(userId string, key string) (Player, error) {
	return p.dao.getPlayerFromDatabase(userId, key)
}
func (p *PlayerLogic) getPlayerInfoHandler(platformId string) (Player, error) {
	// 模拟从数据库中获取玩家信息
	return p.dao.getPlayerFromDatabase(platformId, "pid")
}
func (p *PlayerLogic) unLock(userId string, sid int) (Player, error) {
	playerInfo, err := p.dao.getPlayerFromDatabase(userId, "uid")
	if err != nil {
		return playerInfo, err
	}
	for _, id := range playerInfo.Sids.Ids {
		log.Debug("id is %d", id)
		if id == sid {
			log.Error("id is %d已经解锁了s", id)
			return Player{}, fmt.Errorf("已经解锁过了")
		}
	}
	playerInfo.Sids.Ids = append(playerInfo.Sids.Ids, sid)
	err = db2.Db.Update(TBL_USER,
		bson.M{"uid": userId},
		map[string]interface{}{"$set": map[string]interface{}{"sids": playerInfo.Sids}})
	if err != nil {
		return Player{}, err
	}
	return playerInfo, nil
}
func (p *PlayerLogic) getUserZhuanSun(uid string) int32 {
	return 100
}
func (p *PlayerLogic) AddUserCoins(userId string, coins int) {
	player, _ := p.dao.getPlayerFromDatabase(userId, "uid")
	player.Coins += coins
	p.dao.updateCoins(userId, coins)
}
func (p *PlayerLogic) UpdateLiXianShouYi(userId string, time int64) {
	err := p.dao.updateLastShouRuTime(userId, time)
	if err != nil {
		log.Error("error msg is %v", err)
	}
}
func (p *PlayerLogic) UpdateXuanChuanTilI(userId string, chuanTiLI int) error {
	return p.dao.updateXuanChuanTiLi(userId, chuanTiLI)
}
func (p *PlayerLogic) JoinUnion(userId string, unionId int32) error {
	return p.dao.joinUnion(userId, unionId)
}
func (p *PlayerLogic) UpdateUserShouChong(userId string, shouChong []ShouChong) error {
	return p.dao.updateUserShouChong(userId, shouChong)
}
